package com.youhome.service.service.impl;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.BShareGroup;
import com.youhome.service.model.BShareLog;
import com.youhome.service.response.ShareResult;
import com.youhome.service.service.BShareGroupService;
import com.youhome.service.service.BShareLogService;
import com.youhome.service.service.BShareService;
import com.youhome.utils.IPUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by chen365 on 2019/02/26.
 */
@Service
@Slf4j
public class BShareServiceImpl implements BShareService {
    @Autowired
    private BShareGroupService bShareGroupService;
    @Autowired
    private BShareLogService bShareLogService;

    @Override
    public ShareResult browse(HttpServletRequest request, HttpServletResponse response, String shareId, Integer level) throws CustomException {

        Cookie[] cookies = request.getCookies();
        boolean skip = false;
        boolean isShare = false; //是否已经分享
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                JSONArray jsonArray = JSONArray.fromObject(cookie);
                System.err.println(jsonArray.toString());
                if (cookie.getName().equals(shareId)) {
                    isShare = true;
                    //level = Integer.valueOf(cookie.getValue());
                    if (level < Integer.valueOf(cookie.getValue())) {
                        level++;
                        Cookie coo = new Cookie(shareId, level.toString());
                        coo.setPath("/");
                        response.addCookie(coo);
                        skip = true;
                    }

                    level = Integer.valueOf(cookie.getValue());

                    log.info("重复分享：" + shareId + "<<===========>>" + level);
                }
            }
        }

        if (!isShare) {
            bShareGroupService.addShareGroupHits(shareId); //增加点击数
            level++;
            Cookie cookie = new Cookie(shareId, level.toString());
            cookie.setPath("/");
            response.addCookie(cookie);
            String ip = IPUtils.getIpAddr(request);

            BShareLog shareLog = new BShareLog();
            shareLog.setShareId(shareId);
            shareLog.setLevel(level);
            shareLog.setIp(ip);
            bShareLogService.create(shareLog);
            skip = true;
            log.info("第一次分享：" + shareId + "<<===========>>" + level);
        }
        BShareGroup shareGroup = null;
        if (!skip) {
            shareGroup = bShareGroupService.selectByPrimaryKey(shareId);
            if (shareGroup == null) {
                throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
            }
        }
        log.info("===============分享：" + shareId + "<<====skip：" + skip + "=======>>" + level);
        return new ShareResult(shareId, level, shareGroup, skip);
    }
}
